package com.caiyi.financial.nirvana.sms.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 所有线程池定义
 */
public class ThreadPoolUtil {

    private static Logger logger = LoggerFactory.getLogger(ThreadPoolUtil.class);

    private ThreadPoolUtil() {
    }

    //更新msg线程池
    public static final ThreadPoolExecutor EXECUTOR_UPDATE_MESSAGE = (ThreadPoolExecutor) Executors.newFixedThreadPool(50);
    //更新user余额线程池
    public static final ThreadPoolExecutor EXECUTOR_UPDATE_BALANCE = (ThreadPoolExecutor) Executors.newFixedThreadPool(50);
    //网关发送线程池(行业短信)
    public static final ThreadPoolExecutor EXECUTOR_INDUSTRY_MESSAGE = (ThreadPoolExecutor) Executors.newFixedThreadPool(50);
    //网关发送线程池(营销短信)
    public static final ThreadPoolExecutor EXECUTOR_MARKET_MESSAGE = (ThreadPoolExecutor) Executors.newFixedThreadPool(50);
    //告警短信发送线程池
    public static final ThreadPoolExecutor EXECUTOR_AlARM_MESSAGE = (ThreadPoolExecutor) Executors.newCachedThreadPool();
    //推送状态报告线程池
    public static final ThreadPoolExecutor EXECUTOR_PUSH_MESSAGE = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);

    private static final Map<String, ThreadPoolExecutor> EXECUTOR_MAP = new HashMap<>();

    static {
        EXECUTOR_MAP.put("更新短信状态", EXECUTOR_UPDATE_MESSAGE);
        EXECUTOR_MAP.put("更新用户余额", EXECUTOR_UPDATE_BALANCE);
        EXECUTOR_MAP.put("网关发送[行业]", EXECUTOR_INDUSTRY_MESSAGE);
        EXECUTOR_MAP.put("网关发送[营销]", EXECUTOR_MARKET_MESSAGE);
        EXECUTOR_MAP.put("告警短信发送", EXECUTOR_AlARM_MESSAGE);
        EXECUTOR_MAP.put("状态报告推送", EXECUTOR_PUSH_MESSAGE);
        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1);
        schedule.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                Set<String> keys = EXECUTOR_MAP.keySet();
                for (String key : keys) {
                    ThreadPoolExecutor executor = EXECUTOR_MAP.get(key);
                    long poolSize = executor.getPoolSize();
                    long corePoolSize = executor.getCorePoolSize();
                    long queueSize = executor.getQueue().size();
                    long completedTaskCount = executor.getCompletedTaskCount();
                    if (poolSize > 0) {
                        logger.info("【{}】线程池大小：{}, 核心线程数：{}, 任务队列数：{}, 已完成任务数：{}", key, poolSize, corePoolSize, queueSize, completedTaskCount);
                    }
                }
            }
        }, 0, 60, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        logger.info("5555");
    }
}
